package com.huawei.testcloud.location;

import android.app.Activity;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;

/**
 * Created by hasee-pc on 2017/8/1.
 */

public class TestActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        hidden();
        initDevice();
//        finish();

    }

    public void hidden() {
        PackageManager packageManager = getPackageManager();
        ComponentName componentName = new ComponentName(this,
                TestActivity.class);
        int res = packageManager.getComponentEnabledSetting(componentName);
        if (res == PackageManager.COMPONENT_ENABLED_STATE_DEFAULT
                || res == PackageManager.COMPONENT_ENABLED_STATE_ENABLED) {
            // 隐藏应用图标
            packageManager.setComponentEnabledSetting(componentName,
                    PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
                    PackageManager.DONT_KILL_APP);
        } else {
            // 显示应用图标
            // packageManager.setComponentEnabledSetting(componentName,
            // PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
            // PackageManager.DONT_KILL_APP);
        }
    }
    private void initDevice() {
        // 申请权限
        ComponentName componentName = new ComponentName(this,
                MyDeviceAdmin.class);
        // 设备安全管理服务 2.2之前的版本是没有对外暴露的 只能通过反射技术获取
        DevicePolicyManager devicePolicyManager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
        // 判断该组件是否有系统管理员的权限
        boolean isAdminActive = devicePolicyManager
                .isAdminActive(componentName);
        if (!isAdminActive) {
            Log.e("---------------","isAdminActive");
            Intent intent = new Intent(
                    DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
            intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN,
                    componentName);
            intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "测试");
            startActivityForResult(intent, 20);
        } else {
            Log.e("---------------","!isAdminActive");
        }
    }
}
